package handle.imp;


import DTO.ExcelConfigDTO;
import DTO.ExcelDataDTO;
import handle.ExcelTool;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.text.DecimalFormat;

/**
 * @description: 默认的对Excel中的数据进行读取操作类
 * @author: Ziger
 * @time: 2022/8/26 14:25
 */
public class DefaultExcelTool implements ExcelTool {
    private XSSFSheet sheet;
    private int beginRow;
    private int endRow;
    private int beginColum;
    private int endColum;

    private int currentRow;
    private int currentColum;

    public DefaultExcelTool(ExcelConfigDTO config) throws IOException, InvalidFormatException {
        //FileInputStream fis = new FileInputStream("C:\\Users\\Zi_Ge\\Desktop\\新建 Microsoft Excel 工作表.xlsx");

        XSSFWorkbook workbook = new XSSFWorkbook(config.getExcelFile());
        sheet = workbook.getSheetAt(0);
//        endRow = sheet.getLastRowNum()-1;

        beginRow = config.getBeginRow();
        endRow = config.getEndRow();

        beginColum = config.getBeginColum();
        endColum = config.getEndColum();

        currentColum = config.getCurrentColum();
        currentRow = config.getCurrentRow();

    }

    /**
     * 获取右边的数据
     *
     * @return
     */
    public ExcelDataDTO getNextInfo() {
        try {
            nextColum();
        } catch (Exception e) {
            //读取到最后一个数据
            currentRow = endRow;
            currentColum = endColum;

            return null;
        }

        return getInfo();
    }

    /**
     * 获取左边的数据
     *
     * @return
     */
    public ExcelDataDTO getLastInfo() {
        try {
            lastColum();
        } catch (Exception e) {
            //读取到第一个数据
            currentRow = beginRow;
            currentColum = beginColum;

            return null;
        }

        return getInfo();
    }

    /**
     * 获取下一行的第一个数据
     *
     * @return
     */
    public ExcelDataDTO getNextStu() {
        try {
            nextRow();
        } catch (Exception e) {
            currentRow = endRow;

            return null;
        }

        currentColum = beginColum;
        return getInfo();
    }

    /**
     * 获取上一行的第一个数据
     *
     * @return
     */
    public ExcelDataDTO getLastStu() {
        try {
            lastRow();
        } catch (Exception e) {
            currentRow = beginRow;

            return null;
        }

        currentColum = beginColum;
        return getInfo();
    }

    /**
     * 读取当前光标下数据
     *
     * @return
     */
    private ExcelDataDTO getInfo() {
        XSSFRow row = sheet.getRow(currentRow);
        XSSFCell cell = row.getCell(currentColum);


        String s = getString(cell);


        return new ExcelDataDTO(getRow(), getColum(), s);
    }

    /**
     * 将cell中的数据以String的形式取出
     *
     * @param cell 被读取的cell
     * @return
     */
    private String getString(XSSFCell cell) {
        String s = "";
        try {
            s = cell.getStringCellValue();
        } catch (IllegalStateException e) {
            /**
             * 由于POI会将纯数字表格标记为Double类，采用Format来将纯数字以字符串的形式取出
             */
            DecimalFormat format = new DecimalFormat("#");
            Number value = cell.getNumericCellValue();
            s = format.format(value);
        }
        return s;
    }

    /**
     * 将光标移动至下一列
     *
     * @throws Exception
     */
    private void nextColum() throws Exception {
        currentColum++;
        if (currentColum > endColum) {
            nextRow();
            currentColum = beginColum;
        }
    }

    /**
     * 上一列
     *
     * @throws Exception
     */
    private void lastColum() throws Exception {
        currentColum--;
        if (currentColum < beginColum) {
            lastRow();
            currentColum = endColum;
        }
    }

    /**
     * 下一行
     *
     * @throws Exception
     */
    private void nextRow() throws Exception {
        currentRow++;
        if (currentRow > endRow) {
            throw new Exception("表格已读取完毕");
        }
    }

    /**
     * 上一行
     *
     * @throws Exception
     */
    private void lastRow() throws Exception {
        currentRow--;
        if (currentRow < beginRow) {
            throw new Exception("表格已读取完毕");
        }
    }

    /**
     * 获取表头的列
     *
     * @return
     */
    private String getColum() {
        //由于向用户索要的是纯数据部分，所以表头需要-1
        if (beginRow - 1 >= 0) {
            XSSFRow row = sheet.getRow(beginRow - 1);
            XSSFCell cell = row.getCell(currentColum);
            return getString(cell);
        } else {
            return "无";
        }
    }

    /**
     * 获取表头的行
     *
     * @return
     */
    private String getRow() {
        if (beginColum - 1 >= 0) {
            XSSFRow row = sheet.getRow(currentRow);
            XSSFCell cell = row.getCell(beginColum - 1);
            return getString(cell);
        } else {
            return "无";
        }
    }
}
